<?php
/**
 * 自定义跳转类
 * User: fengyan
 * Date: 17-10-13
 * Time: 上午10:43
 */

namespace App\Helpers;


trait Jump
{
    /**
     * 模板前缀
     * @var string
     */
    private $templatePrefix = '';

    /**
     * 当前模块
     * @var string
     */
    private $module = '';

    protected $data = [];
    /**
     * 设置模板前缀
     * @param string $templatePrefix
     * @return $this
     */
    protected function setTemplatePrefix($templatePrefix = '')
    {
        $this->templatePrefix = $templatePrefix;

        return $this;
    }

    /**
     * 设置当前模块
     * @param string $module
     * @return $this
     */
    protected function setCurrentModule($module = '')
    {
        $this->module = $module;

        return $this;
    }

    public function withData($key, $value)
    {
        $this->data[$key] = $value;

        return $this;
    }

    /**
     * 模板统一调用方法
     * @param null $view
     * @param array $data
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function fetch($view = null, $data = [])
    {
        if (strpos($view, '/') !== 0) {
            $template = $this->templatePrefix . $view;
        } else {
            $template = substr($view, 1);
        }

        $this->templatePrefix = '';

        return response()->view($template, array_merge($this->data, $data));
//        return view($template, $data);
    }

    /**
     * 错误输出
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param int $httpStatus
     * @param array $header
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    protected function error($msg = '', $url = null, $data = '', $wait = 2, $httpStatus = 200, array $header = [])
    {
        if (!empty($url)) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : get_url($this->module, $url);
        }

        $result = [
            'code' => -1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
            'type' => 'success',
        ];

        if (request()->ajax()) {
            return response()->json($result, $httpStatus, $header);
        }

        return $this->fetch('/'.$this->module.'.error', array_merge($result, ['httpStatus'=>$httpStatus]));
    }

    /**
     * 成功返回
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param int $httpStatus
     * @param array $header
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    protected function success($msg = '', $url = null, $data = '', $wait = 2, $httpStatus = 200, array $header = [])
    {
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } elseif ('' !== $url) {
            if ($url != 'no-refresh') {
                $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : get_url($url);
            }
        }

        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
            'type' => 'success',
        ];

        if (request()->ajax()) {
            return response()->json($result, $httpStatus, $header);
        }

        return $this->fetch('/'.$this->module.'.success', array_merge($result, ['httpStatus'=>$httpStatus]));
    }
}